package com.dingding.api.controller.order;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.dingding.api.controller.base.BaseController;
import com.dingding.common.core.base.dto.FacadeResult;
import com.dingding.common.core.base.dto.ResponseEntity;
import com.dingding.common.core.util.JSONUtil;
import com.dingding.common.core.util.paravalidate.ValidateUtil;
import com.dingding.order.common.dto.distance.ReqCalculationDTO;
import com.dingding.order.common.dto.distance.ResCalculationDTO;
import com.dingding.order.dto.phonecall.ReqGetPhoneCall;
import com.dingding.order.dto.phonecall.ResGetPhoneCall;
import com.dingding.order.dto.publish.taxi.ReqPublishOrderByTaxi;
import com.dingding.order.dto.publish.taxi.ResPublishOrderByTaxi;
import com.dingding.order.facade.common.distance.ICalculationFacade;
import com.dingding.order.service.IMarchOrderFacade;
import com.dingding.order.service.IServiceModuleFacade;
import com.dingding.order.service.IUserOrderFacade;

/**
 * 
 * @ClassName: OrderController
 * @Description: 订单模块调度类
 * @author CHEHAUNBO
 * @date 2016年6月3日 下午10:11:19
 * 
 */
@Scope("prototype")
@Controller("orderController")
@RequestMapping("/authorapi/order")
public class OrderController extends BaseController {

	@Resource(name = "userOrderFacade")
	private IUserOrderFacade userOrderFacade;

	@Resource(name = "marchOrderFacade")
	private IMarchOrderFacade marchOrderFacade;

	@Resource(name = "serviceModuleFacade")
	private IServiceModuleFacade serviceModuleFacade;

	@Resource(name = "calculationFacade")
	private ICalculationFacade calculationFacade;
	
	/**
	 * 
	 * 计算距离
	 * 
	 * @Title: publishOrder
	 * @author CHEHAUNBO
	 * 
	 */
	@Deprecated
	@RequestMapping(value = "/v1/computeDistance", method = RequestMethod.POST)
	public void computeDistance(HttpServletRequest request, HttpServletResponse response) {

		// 获取客户端数据
		String clientData = super.getClientData(request, response);
		// 执行业务处理，并返回处理后的数据
		String responseData = userOrderFacade.calculateDistance(clientData);
		// 加密数据，并返回给客户端
		super.responseData(response, responseData);

	}

	/**
	 * 
	 * 获取我的订单
	 * 
	 * @Title: getMyOrder
	 * @author zhouxuan
	 * 
	 */
	@RequestMapping(value = "/v1/getMyOrder", method = RequestMethod.POST)
	public void getMyOrder(HttpServletRequest request, HttpServletResponse response) {

		// 获取客户端数据
		String clientData = super.getClientData(request, response);
		// 执行业务处理，并返回处理后的数据
		String responseData = userOrderFacade.getMyOrder(clientData);
		// 加密数据，并返回给客户端
		super.responseData(response, responseData);

	}

	/**
	 * 
	 * 获取历史订单
	 * 
	 * @Title: getHistoryOrder
	 * @author YHQ
	 * 
	 */
	@RequestMapping(value = "/v1/getHistoryOrder", method = RequestMethod.POST)
	public void getHistoryOrder(HttpServletRequest request, HttpServletResponse response) {

		// 获取客户端数据
		String clientData = super.getClientData(request, response);
		// 执行业务处理，并返回处理后的数据
		String responseData = userOrderFacade.getHistoryOrder(clientData);
		// 加密数据，并返回给客户端
		super.responseData(response, responseData);

	}

	/**
	 * 
	 * 获取历史订单详情
	 * 
	 * @Title: getHistoryOrder
	 * @author YHQ
	 * 
	 */
	@RequestMapping(value = "/v1/historyOrderDetail", method = RequestMethod.POST)
	public void historyOrderDetail(HttpServletRequest request, HttpServletResponse response) {

		// 获取客户端数据
		String clientData = super.getClientData(request, response);
		// 执行业务处理，并返回处理后的数据
		String responseData = userOrderFacade.getHistoryOrderDetail(clientData);
		// 加密数据，并返回给客户端
		super.responseData(response, responseData);

	}

	/**
	 * 出租车计算距离
	 * 
	 * @Title: taxiComputeDistance
	 * @author CHEHAUNBO
	 */
	@RequestMapping(value = "/v1/taxiComputeDistance", method = RequestMethod.POST)
	public void taxiComputeDistance(HttpServletRequest request, HttpServletResponse response) {
		// 获取客户端数据
		String clientData = super.getClientData(request, response);
		// 执行业务处理，并返回处理后的数据
		String responseData = userOrderFacade.taxiCalculateDistance(clientData);
		// 加密数据，并返回给客户端
		super.responseData(response, responseData);
	}

	/**
	 * 
	 * 发布订单
	 * 
	 * @Title: publishOrder
	 * @author CHEHAUNBO
	 * 
	 */
	@RequestMapping(value = "/v1/publishOrder", method = RequestMethod.POST)
	public void publishOrder(HttpServletRequest request, HttpServletResponse response) {

		// 获取客户端数据
		String clientData = super.getClientData(request, response);
		// 执行业务处理，并返回处理后的数据
		String responseData = userOrderFacade.releaseOrder(clientData);
		// 加密数据，并返回给客户端
		super.responseData(response, responseData);

	}

	/**
	 * 
	 * 抢单
	 * 
	 * @Title: grabOrder
	 * @author CHEHAUNBO
	 * 
	 */
	@RequestMapping(value = "/v1/grabOrder", method = RequestMethod.POST)
	public void grabOrder(HttpServletRequest request, HttpServletResponse response) {

		// 获取客户端数据
		String clientData = super.getClientData(request, response);
		// 执行业务处理，并返回处理后的数据
		String responseData = userOrderFacade.userGrabOrder(clientData);
		// 加密数据，并返回给客户端
		super.responseData(response, responseData);

	}

	/**
	 * 
	 * 抢单
	 * 
	 * @Title: taxiGrabOrder
	 * @author YHQ
	 * @version V2.3.0
	 * 
	 */
	@RequestMapping(value = "/v1/taxiGrabOrder", method = RequestMethod.POST)
	public void taxiGrabOrder(HttpServletRequest request, HttpServletResponse response) {

		// 获取客户端数据
		String clientData = super.getClientData(request, response);
		// 执行业务处理，并返回处理后的数据
		String responseData = userOrderFacade.taxiGrabOrder(clientData);
		// 加密数据，并返回给客户端
		super.responseData(response, responseData);

	}

	/**
	 * 
	 * 支付
	 * 
	 * @Title: orderPay
	 * @author CHEHAUNBO
	 * 
	 */
	@RequestMapping(value = "/v1/orderPay", method = RequestMethod.POST)
	public void orderPay(HttpServletRequest request, HttpServletResponse response) {

		// 获取客户端数据
		String clientData = super.getClientData(request, response);
		// 执行业务处理，并返回处理后的数据
		String responseData = userOrderFacade.orderPay(clientData);
		// 加密数据，并返回给客户端
		super.responseData(response, responseData);

	}

	/**
	 * 线上支付
	 * 
	 * @Title: orderPay
	 * @author CHEHAUNBO
	 * 
	 */
	@Deprecated
	@RequestMapping(value = "/v1/taxiOrderOnLinePay", method = RequestMethod.POST)
	public void taxiOrderOnLinePay(HttpServletRequest request, HttpServletResponse response) {

		// 获取客户端数据
		String clientData = super.getClientData(request, response);
		// 执行业务处理，并返回处理后的数据
		String responseData = userOrderFacade.taxiOrderOnLinePay(clientData);
		// 加密数据，并返回给客户端
		super.responseData(response, responseData);
	}
	
	/**
	 * 出租车线上支付
	 * 
	 * @Title: orderPay
	 * @author fengshuonan
	 * 
	 */
	@RequestMapping(value = "/v2/taxiOrderOnLinePay", method = RequestMethod.POST)
	public void taxiOrderOnLinePayV2(HttpServletRequest request, HttpServletResponse response) {

		// 获取客户端数据
		String clientData = super.getClientData(request, response);
		// 执行业务处理，并返回处理后的数据
		String responseData = userOrderFacade.taxiOrderOnLinePayV2(clientData);
		// 加密数据，并返回给客户端
		super.responseData(response, responseData);
	}

	/**
	 * 
	 * 线下支付
	 * 
	 * @Title: orderPay
	 * @author CHEHAUNBO
	 * 
	 */
	@Deprecated
	@RequestMapping(value = "/v1/taxiOrderLinePay", method = RequestMethod.POST)
	public void taxiOrderLinePay(HttpServletRequest request, HttpServletResponse response) {

		// 获取客户端数据
		String clientData = super.getClientData(request, response);
		// 执行业务处理，并返回处理后的数据
		String responseData = userOrderFacade.taxiOrderLinePay(clientData);
		// 加密数据，并返回给客户端
		super.responseData(response, responseData);

	}
	
	/**
	 * 
	 * 线下支付
	 * 
	 * @Title: orderPay
	 * @author CHEHAUNBO
	 * 
	 */
	@RequestMapping(value = "/v2/taxiOrderLinePay", method = RequestMethod.POST)
	public void taxiOrderLinePayV2(HttpServletRequest request, HttpServletResponse response) {

		// 获取客户端数据
		String clientData = super.getClientData(request, response);
		// 执行业务处理，并返回处理后的数据
		String responseData = userOrderFacade.taxiOrderLinePayV2(clientData);
		// 加密数据，并返回给客户端
		super.responseData(response, responseData);

	}

	/**
	 * 
	 * 确认到达
	 * 
	 * @Title: confirmArrive
	 * @author CHEHAUNBO
	 * 
	 */
	@RequestMapping(value = "/v1/confirmArrive", method = RequestMethod.POST)
	public void confirmArrive(HttpServletRequest request, HttpServletResponse response) {

		// 获取客户端数据
		String clientData = super.getClientData(request, response);
		// 执行业务处理，并返回处理后的数据
		String responseData = userOrderFacade.confirmArrive(clientData);
		// 加密数据，并返回给客户端
		super.responseData(response, responseData);

	}

	/**
	 * 
	 * 确认到达
	 * 
	 * @Title: taxiConfirmArrive
	 * @author YHQ
	 * 
	 */
	@RequestMapping(value = "/v1/taxiConfirmArrive", method = RequestMethod.POST)
	public void taxiConfirmArrive(HttpServletRequest request, HttpServletResponse response) {

		// 获取客户端数据
		String clientData = super.getClientData(request, response);
		// 执行业务处理，并返回处理后的数据
		String responseData = userOrderFacade.taxiConfirmArrive(clientData);
		// 加密数据，并返回给客户端
		super.responseData(response, responseData);

	}

	/**
	 * 
	 * 取消订单
	 * 
	 * @Title: cancelOrder
	 * @author CHEHAUNBO
	 * 
	 */
	@RequestMapping(value = "/v1/cancelOrder", method = RequestMethod.POST)
	public void cancelOrder(HttpServletRequest request, HttpServletResponse response) {

		// 获取客户端数据
		String clientData = super.getClientData(request, response);
		// 执行业务处理，并返回处理后的数据
		String responseData = userOrderFacade.cancelOrder(clientData);
		// 加密数据，并返回给客户端
		super.responseData(response, responseData);

	}

	/**
	 * 
	 * 出租车取消订单
	 * 
	 * @Title: taxiCancelOrder
	 * @author YHQ
	 * @since V2.3.0
	 * 
	 */
	@RequestMapping(value = "/v1/taxiCancelOrder", method = RequestMethod.POST)
	public void taxiCancelOrder(HttpServletRequest request, HttpServletResponse response) {

		// 获取客户端数据
		String clientData = super.getClientData(request, response);
		// 执行业务处理，并返回处理后的数据
		String responseData = userOrderFacade.taxiCancelOrder(clientData);
		// 加密数据，并返回给客户端
		super.responseData(response, responseData);

	}

	/**
	 * 
	 * 进行中订单
	 * 
	 * @Title: makeOrder
	 * @author chenwei
	 * 
	 *         修改说明： 由于当前代码重构是在新版本发布之前，故不存在兼容性问题，所以重构之前的代码不再保存 修改人：CHEHUANBO
	 *         修改原因：代码逻辑存在问题，重构代码 修改时间：2016-06-12 修改版本：V2.2.0
	 * 
	 */
	@RequestMapping(value = "/v1/makeOrder", method = RequestMethod.POST)
	public void makeOrder(HttpServletRequest request, HttpServletResponse response) {

		String clientData = super.getClientData(request, response);
		// 执行业务处理，并返回处理后的数据
		String responseData = marchOrderFacade.getMarchOrder(clientData);
		// 加密数据，并返回给客户端
		super.responseData(response, responseData);
	}

	/**
	 * 
	 * 附近订单
	 * 
	 * @Title: nearbyOrder
	 * @author CHEHAUNBO
	 * 
	 */
	@RequestMapping(value = "/v1/nearbyOrder", method = RequestMethod.POST)
	public void nearbyOrder(HttpServletRequest request, HttpServletResponse response) {

		// 获取客户端数据
		String clientData = super.getClientData(request, response);
		// 执行业务处理，并返回处理后的数据
		String responseData = userOrderFacade.getNearOrderList(clientData);
		// 加密数据，并返回给客户端
		super.responseData(response, responseData);

	}

	/**
	 * 
	 * 出租车获取附近订单
	 * 
	 * @Title: nearbyOrder
	 * @author CHEHAUNBO
	 * @since V2.3.0
	 * 
	 */
	@RequestMapping(value = "/v1/taxiGetNearbyOrder", method = RequestMethod.POST)
	public void taxiGetNearbyOrder(HttpServletRequest request, HttpServletResponse response) {

		// 获取客户端数据
		String clientData = super.getClientData(request, response);
		// 执行业务处理，并返回处理后的数据
		String responseData = userOrderFacade.taxiGetNearbyOrderList(clientData);
		// 加密数据，并返回给客户端
		super.responseData(response, responseData);

	}

	/**
	 * 
	 * 我的(历史)订单
	 * 
	 * 暂时搁置，使用旧的接口（Che）
	 * 
	 * @Title: nearbyOrder
	 * @author CHEHAUNBO
	 * 
	 */
	@RequestMapping(value = "/v1/historyOrder", method = RequestMethod.POST)
	public void userHistoryOrder(HttpServletRequest request, HttpServletResponse response) {

		// 获取客户端数据
		// String clientData = super.getClientData(request, response);

	}

	/**
	 * @Description:发布出租车订单
	 * @author fengshuonan
	 * @return void
	 */
	@Deprecated
	@RequestMapping(value = "/v1/publishTaxiOrder", method = RequestMethod.POST)
	public void publishTaxiOrder(HttpServletRequest request, HttpServletResponse response) {

		// 获取客户端数据
		String clientData = super.getClientData(request, response);

		// 执行业务处理，并返回处理后的数据
		String responseData = this.userOrderFacade.publishTaxiOrder(clientData);

		// 加密数据，并返回给客户端
		super.responseData(response, responseData);
	}

	/**
	 * @Description:发布出租车订单
	 * @author YHQ
	 * @since V2.5.6
	 * @return void
	 */
	@RequestMapping(value = "/v1/publishOrderByTaxi", method = RequestMethod.POST)
	public void publishOrderByTaxi(HttpServletRequest request, HttpServletResponse response) {
		
		// 获取客户端数据
		ReqPublishOrderByTaxi reqPublishOrderByTaxi = super.getRequestBean(request, response,ReqPublishOrderByTaxi.class);
		
		// 执行业务处理，并返回处理后的数据
		ResponseEntity<ResPublishOrderByTaxi> responseEntity = this.userOrderFacade.publishOrderByTaxi(reqPublishOrderByTaxi);
		
		// 加密数据，并返回给客户端
		this.responseData(response, JSONUtil.toJSONString(responseEntity, "yyyy-MM-dd HH:mm:ss"));
		
	}
	
	/**
	 * @Description: 去支付
	 * @author fengshuonan
	 * @since 2.3.0
	 */
	@RequestMapping(value = "/v1/goToPay", method = RequestMethod.POST)
	public void goToPay(HttpServletRequest request, HttpServletResponse response) {

		// 获取客户端数据
		String clientData = super.getClientData(request, response);

		// 执行业务处理，并返回处理后的数据
		String responseData = this.userOrderFacade.goToPay(clientData);

		// 加密数据，并返回给客户端
		super.responseData(response, responseData);

	}

	/**
	 * @Description: 查询订单支付结果
	 * @author fengshuonan
	 * @since 2.3.0
	 */
	@RequestMapping(value = "/v1/queryOrderResult", method = RequestMethod.POST)
	public void queryOrderResult(HttpServletRequest request, HttpServletResponse response) {

		// 获取客户端数据
		String clientData = super.getClientData(request, response);

		// 执行业务处理，并返回处理后的数据
		String responseData = this.userOrderFacade.queryPayResult(clientData);

		// 加密数据，并返回给客户端
		super.responseData(response, responseData);
	}

	/**
	 * @Description:获取服务城市列表
	 * @author fengshuonan
	 * @since 2.4
	 */
	@RequestMapping(value = "/v1/getServiceCities", method = RequestMethod.POST)
	public void getServiceCities(HttpServletRequest request, HttpServletResponse response) {

		// 获取客户端数据
		String clientData = super.getClientData(request, response);

		String responseData = this.serviceModuleFacade.getServiceCities(clientData);

		// 加密数据，并返回给客户端
		super.responseData(response, responseData);
	}

	/**
	 * 专车计算距离
	 * 
	 * @Title: specialCalculatedDistance
	 * @author YHQ
	 */
	@RequestMapping(value = "/v1/specialCalculatedDistance", method = RequestMethod.POST)
	public void specialCalculatedDistance(HttpServletRequest request, HttpServletResponse response) {
		// 获取客户端数据
		String clientData = super.getClientData(request, response);
		// 执行业务处理，并返回处理后的数据
		String responseData = userOrderFacade.specialCalculateDistance(clientData);
		// 加密数据，并返回给客户端
		super.responseData(response, responseData);
	}

	/**
	 * @Description:发布专车订单
	 * @author YHQ
	 * @since V2.4.0
	 * @return void
	 */
	@RequestMapping(value = "/v1/publishSpecialOrder", method = RequestMethod.POST)
	public void publishSpecialOrder(HttpServletRequest request, HttpServletResponse response) {

		// 获取客户端数据
		String clientData = super.getClientData(request, response);

		// 执行业务处理，并返回处理后的数据
		String responseData = this.userOrderFacade.publishSpecialOrder(clientData);

		// 加密数据，并返回给客户端
		super.responseData(response, responseData);
	}

	/**
	 * @Description:取消专车订单
	 * @author YHQ
	 * @since V2.4.0
	 * @return void
	 */
	@RequestMapping(value = "/v1/passengerCancelSpecialOrder", method = RequestMethod.POST)
	public void passengerCancelSpecialOrder(HttpServletRequest request, HttpServletResponse response) {

		// 获取客户端数据
		String clientData = super.getClientData(request, response);

		// 执行业务处理，并返回处理后的数据
		String responseData = this.userOrderFacade.specialCancelOrder(clientData);

		// 加密数据，并返回给客户端
		super.responseData(response, responseData);
	}

	/**
	 * @Description:获取电话被叫号码
	 * @author fengshuonan
	 * @since 2.4
	 */
	@RequestMapping(value = "/v1/getPhoneCall", method = RequestMethod.POST)
	public void getPhoneCall(HttpServletRequest request, HttpServletResponse response) {
		
		ReqGetPhoneCall reqGetPhoneCall = super.getRequestBean(request, response, ReqGetPhoneCall.class);
		
		//验证参数是否为空
		if(ValidateUtil.validateBean(reqGetPhoneCall, ReqGetPhoneCall.class) == false){
			super.generateNullParamRes(response);
		}
		
		FacadeResult<ResGetPhoneCall> phoneCall = this.userOrderFacade.getPhoneCall(reqGetPhoneCall);
		
		super.generateFacadeResponse(response, new ResponseEntity<ResGetPhoneCall>(), phoneCall);

	}

	/**
	 * @Description:计算距离价格
	 * @author YHQ
	 * @since 2.5.7
	 */
	@RequestMapping(value = "/v1/calculatedDistance", method = RequestMethod.POST)
	public void calculatedDistance(HttpServletRequest request, HttpServletResponse response){
		
		// 获取验证码
		ReqCalculationDTO reqCalculationDTO = super.getRequestBean(request, response, ReqCalculationDTO.class);
		
		// 计算距离价格响应Bean
		ResponseEntity<ResCalculationDTO> responseEntity = calculationFacade.calculationMileageCost(reqCalculationDTO,new ResponseEntity<ResCalculationDTO>());
		
		this.responseData(response, responseEntity);
	}
	
}
